﻿using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.DirectX;

namespace NGE
{
    public class AnimationComponent : Component
    {
        public AnimationComponent(string name, Game g)
            : base(name, g)
        {
        }

        List<string> aniList = new List<string>();
        List<int> aniPlaytimes = new List<int>();
        List<bool> aniIsFinished = new List<bool>();
        List<bool> aniAutoClear = new List<bool>();
        List<bool> aniIsFlip = new List<bool>();

        void removeAni(int i)
        {
            //for (int i = 0; i < aniList.Count; i++)
            {
                //if (aniList[i] == name)
                {
                    //if (aniAutoClear[i])
                    {
                        aniIsFinished.RemoveAt(i);
                        aniAutoClear.RemoveAt(i);
                        aniList.RemoveAt(i);
                        aniPlaytimes.RemoveAt(i);
                        aniIsFlip.RemoveAt(i);
                    }
                    //return;
                }
            }
        }
        void stopAni(int i)
        {
            //for (int i = 0; i < aniList.Count; i++)
            {
                //if (aniList[i] == name)
                {
                    aniIsFinished[i] = true;
                }
            }
        }
        bool addAni(string regName, int times, bool autoclr,bool isflip)
        {
            //for (int i = 0; i < aniList.Count; i++)
            //{
            //    if (aniList[i] == regName)
            //    {
            //        //aniPlaytimes[i] = times;
            //        //aniAutoClear[i] = autoclr;
            //        //aniIsFinished[i] = false;
            //        if (isflip == aniIsFlip[i])
            //        {
            //            return false;
            //        }
            //        else
            //        {
            //            aniIsFlip[i] = isflip;
            //            return true;
            //        }
            //    }
            //}
            if (aniList.Contains(regName))
            {
                return false;
            }

            aniList.Add(regName);
            aniPlaytimes.Add(times);
            aniAutoClear.Add(autoclr);
            aniIsFinished.Add(false);
            aniIsFlip.Add(isflip);

            return true;
        }

        public bool Contains(string aniname)
        {
            for (int i = 0; i < aniList.Count; i++)
            {
                if (aniList[i] == aniname)
                {
                    return true;
                }
            }
            return false;
        }

        public bool AnimationIsStoped(string name)
        {
            for (int i = 0; i < aniList.Count; i++)
            {
                if (aniList[i] == name)
                {
                    return aniIsFinished[i];
                }
            }
            return true;
        }

        public override void Heartbeat()
        {
            base.Heartbeat();
        }

        public override void Show()
        {
            base.Show();

            for (int i = 0; i < aniList.Count; i++)
            {
                string name = aniList[i];
                if (game.Graph.GetAnimationPaused(name))
                {
                    if (aniPlaytimes[i] < 0)
                    {
                        game.Graph.AnimationReset(name);
                    }
                    else
                    {
                        int times = game.Graph.GetAnimationPlayedTimes(name);
                        if (times == aniPlaytimes[i])
                        {
                            if (aniAutoClear[i])
                            {
                                removeAni(i);
                                game.Graph.UnloadAnimation(name);
                                continue;
                            }
                            else
                            {
                                stopAni(i);
                                game.Graph.AnimationPause(name);
                            }
                        }
                        else
                        {
                            game.Graph.AnimationReset(name);
                            //game.Graph.DrawAnimation(name);
                        }
                    }
                }
                game.Graph.DrawAnimation(name, aniIsFlip[i]);
            }
        }

        public void ClearAnimation(string regName)
        {
            for (int i = 0; i < aniList.Count; i++)
            {
                if (aniList[i] == regName)
                {
                    game.Graph.UnloadAnimation(regName);
                    removeAni(i);
                    return;
                }
            }
        }
        public void ClearAnimationContains(string contains)
        {
            for (int i = 0; i < aniList.Count; i++)
            {
                if (aniList[i].Contains(contains))
                {
                    game.Graph.UnloadAnimation(aniList[i]);
                    removeAni(i);
                    i--;
                }
            }
        }

        public void DrawAnimation(string regName, string fileName, Vector3 loc, int times, bool autoclr, bool isflip)
        {
            DrawAnimation(regName, fileName, loc, loc, times, autoclr, 1, isflip);
        }
        public void DrawAnimation(string fileName, Vector3 loc, int times, bool autoclr, bool isflip)
        {
            DrawAnimation(fileName, fileName, loc, loc, times, autoclr, 1, isflip);
        }
        public void DrawAnimation(string fileName, Vector3 from, Vector3 to, int times, bool autoclr, int delay, bool isflip)
        {
            DrawAnimation(fileName, fileName, from, to, times, autoclr, delay, isflip);
        }
        public void DrawAnimation(string regName, string fileName, Vector3 from, Vector3 to, int times, bool autoclr, int delay, bool isflip)
        {
            if (fileName == null || fileName == string.Empty)
            {
                return;
            }
            if (addAni(regName, times, autoclr,isflip))
            {
                game.Graph.LoadAnimation(regName, fileName, from, to, delay);
            }
        }


        public void DrawAnimation(string regName, NAnimation nani, Vector3 loc, int times, bool autoclr, bool isflip)
        {
            DrawAnimation(regName, nani, loc, loc, times, autoclr, 1,isflip);
        }
        public void DrawAnimation(string regName, NAnimation nani, Vector3 from, Vector3 to, int times, bool autoclr, int delay, bool isflip)
        {
            if (nani == null)
            {
                return;
            }
            if (addAni(regName, times, autoclr,isflip))
            {
                game.Graph.LoadAnimation(regName, nani, from, to, delay);
            }
        }
   }
}
